
package saxprocessing;

//import thu vien
import java.io.File;
import java.util.jar.Attributes;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXProcessing extends DefaultHandler {

    //khoi tao bien count
    static int count = 0;
    public static void main(String[] args) {
        //If tham so truyen vao ham main, neu #1
//        if(args.length != 1){
//            System.out.println("Usage: java SAXProsessing tomcat-users.xml");
//            System.exit(1);
//        }
        //Khoi tao doi tuong ua lop SAXProcessing
        SAXProcessing saxObject = new SAXProcessing();

        //lookup procedure to determine
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            //Khoi tao phuong thuc parser(doc file)
            SAXParser parser = spf.newSAXParser();
            //Tro den ten file can doc, cung object
            parser.parse(SAXProcessing.class.getResourceAsStream("tomcat-users.xml"), saxObject);
        } catch (Exception e) {
            e.printStackTrace();
            //exit khoi ham quay lai ham main
            System.exit(1);
        }
    }

    //The StartElement interface provides access to information about start elements.
    //A StartElement is reported for each Start Tag in the document.
    @Override
    public void startElement(String uri, String localName, String qualifiedName, org.xml.sax.Attributes attributes) throws SAXException {
        if(qualifiedName.equals("user")){
            count++;
        }
    }

//    public void startElement(String namespaceURI, String simpleName, String qualifiedName, Attributes attributeList)throws SAXException{
//        
//    }

    public void endDocument() throws SAXException{
        System.out.print(count+ " -users defined on Tomcat server");
    }

}
